package com.fintek.mx.mxMarket.base


import android.app.Activity
import android.os.Bundle
import com.adjust.sdk.Adjust
import com.adjust.sdk.AdjustConfig
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.Utils
import com.fintek.mx.mxMarket.R
import com.fintek.mx.mxMarket.util.ZZToast
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import me.hgj.jetpackmvvm.base.BaseApp
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.Objects


class MxApplication : BaseApp() {

    //static 代码段可以防止内存泄露
    init {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(
                R.color.translation,
                R.color.black63
            )//全局设置主题颜色
            ClassicsHeader(context)//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            layout.setPrimaryColorsId(
                R.color.translation,
                R.color.black63
            )
            ClassicsFooter(context).also {
                //指定为经典Footer，默认是 BallPulseFooter
                it.setDrawableSize(20f)
            }
        }
    }

    override fun onCreate() {
        super.onCreate()
        EventBus.getDefault().register(this)
        ZZToast.init(this)
        Utils.init(this)

        val appToken = AccountConfig.adjust_token
        val environment = AdjustConfig.ENVIRONMENT_PRODUCTION
        val config = AdjustConfig(this, appToken, environment)
        Adjust.onCreate(config)
        registerActivityLifecycleCallbacks(SwitchBackgroundCallbacks())
    }

    override fun onTerminate() {
        super.onTerminate()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe
    fun needLogin(event: LoginEvent) {
    }

    /*Adjust 会话追踪*/
    private class SwitchBackgroundCallbacks : ActivityLifecycleCallbacks {
        override fun onActivityCreated(p0: Activity, p1: Bundle?) {
        }

        override fun onActivityStarted(p0: Activity) {

        }

        override fun onActivityResumed(p0: Activity) {
            Adjust.onResume();
        }

        override fun onActivityPaused(p0: Activity) {
            Adjust.onPause();
        }

        override fun onActivityStopped(p0: Activity) {

        }

        override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {

        }

        override fun onActivityDestroyed(p0: Activity) {

        }

    }
}

class LoginEvent